*** This code graphs bounds by age

cap log close
clear
set more off
set type double
set linesize 150

log using "$logdir/analysis/graph_bounds_by_age.log" , replace


local thresh 100


****************
* Prepare data *
****************

use "$statadir/soi_cdw_taxsim_2019.dta" if nonfiler==0 , clear

count

quietly do "$adir/get_bad_dummies"
get_bad_dummies
*NOTE: this calculates the item-based failure indicators at the default tolerance of $100

gen byte good_any = 1 - bad_any

drop bad_any

gen double age_prim = 2019 - soi_prim_yob if !missing(soi_prim_yob) & soi_prim_yob!=0

foreach xx of numlist 1 4 {
	gen double delta_tax`xx' = taxsim`xx'_fed_incm_tax - soi_fed_incm_tax
	
	gen byte d_correct`thresh'_`xx' = (abs(delta_tax`xx')<`thresh')
}



************************
* Graph: bounds by age *
************************

preserve

	*** Item-based and tax-liability approaches, by age ***
	
	keep if inrange(age_prim,18,90)
	
	collapse (mean) good_any d_correct`thresh'_1 [aw=soi_wgt] , by(age_prim) fast
	
	twoway (line good_any            age_prim , lc(navy)   lp(dash) ) ///
		   (line d_correct`thresh'_1 age_prim , lc(ebblue) lp(solid)) , ///
		legend(order(1 "Item-based approach"  2 "Tax-liability approach")) ///
		ytitle("Share successful") ylabel(0(0.2)1 , format(%2.1fc) nogrid) ///
		xtitle("Age of primary filer") graphregion(fcolor(white)) ///
		xlabel(20(10)90 , format(%9.0f) nogrid)
	graph export "$outdir/figures/bounds_by_age.png" , width(1000) replace
restore


cap log close
